
#include "sample_sys.h"
#include "sample_vin.h"
#include "sample_vout.h"
#include "sample_isp.h"
#include "sample_cap.h"


int main(int argc, char **argv)
{
    int ret = -1;
	int count = 0,offset = 0,read_i =0;
    int fd_sav;
    char openfile[40], filedir[30];
	char *filename = argv[4];
	int uv_size=0;
	int yuv_type = atoi(argv[2]);

	int fd = -1;
    struct stat fstatbuf;
    int rlen	= 0;
    int size	= 0;
    sgks_mpi_vdec_info_s vdec_info;
    frame_head_t frame_head;
	int num = 0;
    int pos = 0;
	char *data_buf = (char *)sgks_mpi_Malloc(sizeof(char) * 1024 * 1024);

	printf("**********************************************************************\n");
	printf("* sgks mpi sample cap svn version: %s  build time: %s  \r\n",SVNREVISION,TIMESTAMP);
	printf("**********************************************************************\n");

	sgks_mpi_cap_yuv_info_s yuv_info;
	yuv_info.source_type = (sgks_mpi_cap_yuv_source_type_e)yuv_type;

	sgks_mpi_cap_yuv_rescale_s   yuv_rescale;
	memset(&yuv_rescale, 0, sizeof(sgks_mpi_cap_yuv_rescale_s));

	if (argc < 3)
	{	
		printf(" Input parameter error \n\r");
		printf(" 1.<enc> cvbs 2.<enc> vin 3.<enc> rescale 4.<dec> source size 5.<dec> screen size 6.<dec> user config size \n\r");
		printf("./sample_cap <enc> <1/2/3> <rescale_width>(only 3) <rescale_height>(only 3) \n\r");
		printf("./sample_cap <dec> <4/5/6> <h264/mjpeg> <file_name> <file_width> <file_height> <rescale_width>(only 6) <rescale_height>(only 6)\n\r");
		return -1;
	}

    if(strcmp(argv[1], "dec") == 0)
    {
    	if (argc < 9)
	    {
	    	      /* ./sample-cap dec yuv_type h264 /home/1080_h264_ch0.h264 1920 1080 */
	        printf("./sample_main <dec> <yuv_type> <h264/mjpeg> <filename> <width> <height> <rescale_width>(only 6) <rescale_height>(only 6)\n");
				  
	        return -1;
	    }

        /*only dec*/
        ret = sgks_mpi_sample_SysInit(SGKS_DSP_DECODE_MODE);
        if (ret != SGKS_SUCCESS)
            return ret;

	    if(strcmp(argv[3], "h264") == 0)
	    {
			if(strcmp(argv[2], "4") == 0)
			{
				ret = sgks_mpi_sample_VoutInit(SGKS_VO_SRC_YUV422, SGKS_VO_SRC_ENC);
		        if (ret != SGKS_SUCCESS)
		            return ret;
			}
			else 
			{
				ret = sgks_mpi_sample_VoutInit(SGKS_VO_SRC_DEC, SGKS_VO_SRC_ENC);
		        if (ret != SGKS_SUCCESS)
		            return ret;
			}

	        ret =  sgks_mpi_sys_Start();
	        if (ret != SGKS_SUCCESS)
	            return ret;

			ret = sgks_mpi_sample_CapInit(strtoul(argv[2], NULL, 0),1,strtoul(argv[7], NULL, 0),strtoul(argv[8], NULL, 0));
			if (ret != SGKS_SUCCESS)
				return ret;	

	        ret = sgks_mpi_sample_EnableH264Cap(strtoul(argv[5], NULL, 0), strtoul(argv[6], NULL, 0));
	        if (ret != SGKS_SUCCESS)
	            return ret;

	    }
	    else if(strcmp(argv[3], "mjpeg") == 0)
	    {

	        ret = sgks_mpi_sample_VoutInit(SGKS_VO_SRC_DEC, SGKS_VO_SRC_ENC);
	        if (ret != SGKS_SUCCESS)
	            return ret;

	        ret =  sgks_mpi_sys_Start();
	        if (ret != SGKS_SUCCESS)
	            return ret;

			ret = sgks_mpi_sample_CapInit(strtoul(argv[2], NULL, 0),1,strtoul(argv[7], NULL, 0),strtoul(argv[8], NULL, 0));
			if (ret != SGKS_SUCCESS)
				return ret;	
			
	        ret = sgks_mpi_sample_EnableMjpegCap(strtoul(argv[5], NULL, 0), strtoul(argv[6], NULL, 0), strtoul(argv[7], NULL, 0), strtoul(argv[8], NULL, 0));
	        if (ret != SGKS_SUCCESS)
	            return ret;

	    }
		
	    if ( data_buf == NULL)
	    {
	        printf("data_buf == NULL\n");
	        return -1;
	    }

	    fd = open(filename, O_RDWR);
	    if (fd <= 0)
	    {
	        printf("open file err!\n");
	        sgks_mpi_Free(data_buf);
	        return -1;
	    }

	    fstat(fd ,&fstatbuf);

    }   
	else if(strcmp(argv[1], "enc") == 0)
    {

		if (argc < 5)
	    {
	    	      /* ./sample-cap enc yuv_type h264 /home/1080_h264_ch0.h264 1920 1080 */
	        printf("./sample_main <enc> <yuv_type> <rescale_width>(only 3) <rescale_height>(only 3) \n");
	        return -1;
	    }
	
         /*enc --> dec*/
		ret = sgks_mpi_sample_SysInit(SGKS_DSP_ENCODE_MODE);
		if (ret != SGKS_SUCCESS)
			return ret;

		ret = sgks_mpi_sample_VinInit();
		if (ret != SGKS_SUCCESS)
			return ret;

		ret = sgks_mpi_sample_IspInit();
		if (ret != SGKS_SUCCESS)
			return ret;

    	ret = sgks_mpi_sample_VoutInit(SGKS_VO_SRC_ENC,SGKS_VO_SRC_ENC);
		if (ret != SGKS_SUCCESS)
			return ret;	
		
		ret =  sgks_mpi_sys_Start();
		if (ret != SGKS_SUCCESS)
			return ret;

		ret = sgks_mpi_sample_CapInit(strtoul(argv[2], NULL, 0),1,strtoul(argv[3], NULL, 0),strtoul(argv[4], NULL, 0));
		if (ret != SGKS_SUCCESS)
			return ret;	

		sgks_mpi_sys_ViBindVo(1, 0); 
    }
	
	system("rm -rf /home/yuv");
	umask(~(S_IRWXU|S_IRWXO|S_IRWXG));
    system("mkdir /home/yuv");
    strcpy(filedir, "/home/yuv/");
	
	while(1)
	{
		if(strcmp(argv[1], "dec") == 0)
		{
			if (pos >= fstatbuf.st_size)
	        {
	            lseek(fd, 0, SEEK_SET);
	            pos = 0;
	            printf("start begin..............\n");
	        }
			
	        //read head
	        rlen = read(fd, &frame_head,sizeof(frame_head_t));
	        if(rlen != sizeof(frame_head_t))
	        {
	            printf("error read frame head\n");
	            pos = fstatbuf.st_size;
	            continue;
	        }
	        pos += rlen;

	        //read frame size
	        rlen = read(fd, data_buf, frame_head.frame_size);
	        if(rlen != frame_head.frame_size)
	        {
	            printf("error read the frame\n");
	            pos = fstatbuf.st_size;
	            continue;
	        }
	        pos += rlen;
	        size=frame_head.frame_size;

	        ret = sgks_mpi_vdec_SendData((u8*)data_buf, size);
	        if (ret < 0)
	        {
	            printf("sgks_mpi_vdec_SendData failed! ret=%d\n", ret);
	        }

			ret = sgks_mpi_vdec_GetDecInfo(&vdec_info);
	        if(ret < 0)
	        {
	            printf("SGKS_Get_VideoDecodeInfo failed!\n");
	        }

	        
		}

		/* ??YUV???? */
		ret = sgks_mpi_cap_GetData(&yuv_info);
		/* ????????,??????? ,????????????YUV?? */

		if(ret >= 0) 
		{
			if((yuv_info.yAddr != NULL)&&(yuv_info.uvAddr != NULL))
			{
#if 1
				sprintf(openfile, "%s%dx%d_%d.yuv",filedir, yuv_info.width, yuv_info.height, count);
				fd_sav = open(openfile, O_CREAT|O_RDWR,S_IRWXU|S_IRWXO|S_IRWXG);
				if(fd_sav < 0)
				{
					printf("open file:%s error:%d\n", openfile, fd_sav);
					break;
				}

				//save y data
				write(fd_sav, yuv_info.yAddr,yuv_info.width*yuv_info.height);
				uv_size=(yuv_info.frameFormat==SGKS_CAP_YUV_SEMIPLANAR_420) ? yuv_info.width*yuv_info.height/2 : yuv_info.width*yuv_info.height;
				//sav uv data
				write(fd_sav, yuv_info.uvAddr,uv_size);
				close(fd_sav);
#endif
				printf("==read.y_addr:0x%x,uv_addr:0x%x,witdth:%d,height:%d,yuv_type:%d,uv_size:%d\n",
					   yuv_info.yAddr,yuv_info.uvAddr,yuv_info.width,yuv_info.height,yuv_info.frameFormat,uv_size);

				if(count++ >= 1000)
                {
                    break;
                }
			}
		}
		usleep(20*1000);
	}	

    return SGKS_SUCCESS;
}




